package com.zmn.brs.admin.controller.whitelist;

import com.zmn.brs.model.entity.whitelist.Whitelist;
import com.zmn.brs.model.entity.whitelist.query.WhitelistQuery;
import com.zmn.brs.services.interfaces.blacklist.BlacklistService;
import com.zmn.brs.services.interfaces.whitelist.WhitelistService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
 * 描述: 白名单页面控制类
 *
 * @author wangjie
 * @since 2019-08-01
 **/
@Controller
@RequestMapping(value = "/whitelist/")
public class WhitelistListController extends WhitelistBaseController {

    public WhitelistListController(WhitelistService whitelistService, BlacklistService blacklistService) {
        super(whitelistService, blacklistService);
    }

    /**
     * 描述: 白名单列表跳转，加载方法
     *
     * @param query
     *            查询条件
     * @return org.springframework.web.servlet.ModelAndView
     **/
    @RequestMapping(value = "list")
    public ModelAndView list(@ModelAttribute WhitelistQuery query) {

        ModelAndView mv = new ModelAndView("whitelist/list");
        List<Whitelist> blacklistArr = Optional.ofNullable(whitelistService.listPageByQuery(query)).orElse(Collections.emptyList());
        mv.addObject("items", blacklistArr);
        mv.addObject("vo", query);
        mv.addObject("whitelistTypeList", getWhitelistTypeList());
        mv.addObject("title", getWhitelistTypeTitleByTypeNum(query.getWhitelistType()));
        return mv;
    }

    /**
     * 描述: 添加白名单IP页面跳转
     *
     * @return org.springframework.web.servlet.ModelAndView
     **/
    @RequestMapping(value = "add_whitelist")
    // @RequiresPermissions(value = "/whitelist/add_whitelist.action")
    public ModelAndView add(@RequestParam(value = "whitelistType", defaultValue = "0") Integer whitelistType) {
        ModelAndView mv = new ModelAndView("whitelist/add");
        mv.addObject("whitelistType", whitelistType);
        mv.addObject("title", getWhitelistTypeTitleByTypeNum(whitelistType));
        return mv;
    }
}